package com.lonely.goods.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lonely.common.core.domain.Select;
import com.lonely.common.core.page.Condition;
import com.lonely.common.enums.CommonStatus;
import com.lonely.common.utils.SecurityUtils;
import com.lonely.goods.domain.dto.supplier.QuerySupplierPageDTO;
import com.lonely.goods.domain.dto.supplier.SupplierSubmitDTO;
import com.lonely.goods.domain.entity.SysSupplier;
import com.lonely.goods.domain.vo.supplier.SupplierPageQueryVO;
import com.lonely.goods.mapper.SysSupplierMapper;
import com.lonely.goods.service.ISupplierService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : 黄志标
 * @date: 2025/7/2 14:44
 * @description: 供应商服务实现类
 */
@Service
public class SupplierServiceImpl extends ServiceImpl<SysSupplierMapper, SysSupplier> implements ISupplierService {


    /**
     * 分页查询
     *
     * @param queryDto
     * @return
     */
    @Override
    public Page<SupplierPageQueryVO> page(QuerySupplierPageDTO queryDto) {
        Page<SysSupplier> page = Condition.page(queryDto);
        LambdaQueryWrapper<SysSupplier> queryWrapper = new LambdaQueryWrapper<SysSupplier>()
                .like(StrUtil.isNotBlank(queryDto.getName()), SysSupplier::getName, queryDto.getName())
                .like(StrUtil.isNotBlank(queryDto.getPhone()), SysSupplier::getPhone, queryDto.getPhone())
                .eq(queryDto.getStatus() != null, SysSupplier::getStatus, queryDto.getStatus());

        Page<SysSupplier> supplierPage = super.page(page, queryWrapper);
        Page<SupplierPageQueryVO> resultPage = new Page<>();
        BeanUtils.copyProperties(supplierPage, resultPage);

        if (CollUtil.isNotEmpty(supplierPage.getRecords())) {
            resultPage.setRecords(supplierPage.getRecords().stream().map(x -> {
                SupplierPageQueryVO supplierVO = new SupplierPageQueryVO();
                BeanUtils.copyProperties(x, supplierVO);
                return supplierVO;
            }).collect(Collectors.toList()));
        }
        return resultPage;
    }

    /**
     * 创建供应商
     *
     * @param submitDto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(SupplierSubmitDTO submitDto) {
        Assert.isNull(submitDto.getId(), "参数错误(id)");
        checkSupplier(submitDto);

        SysSupplier sysSupplier = new SysSupplier();
        BeanUtils.copyProperties(submitDto, sysSupplier);
        sysSupplier.setCreateBy(SecurityUtils.getUserId());
        sysSupplier.setUpdateBy(SecurityUtils.getUserId());
        super.save(sysSupplier);
    }

    /**
     * 编辑供应商
     *
     * @param submitDto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(SupplierSubmitDTO submitDto) {
        Assert.notNull(submitDto.getId(), "参数错误(id)");
        checkSupplier(submitDto);
        SysSupplier oldSupplier = super.getById(submitDto.getId());
        BeanUtils.copyProperties(submitDto, oldSupplier);
        oldSupplier.setUpdateBy(SecurityUtils.getUserId());
        super.updateById(oldSupplier);
    }

    /**
     * 启用供应商
     *
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void enabled(Long id) {
        SysSupplier supplier = super.getById(id);
        Assert.notNull(supplier, "供应商不存在");
        supplier.setStatus(CommonStatus.ENABLE.code);
        super.updateById(supplier);
    }

    /**
     * 禁用供应商
     *
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void disable(Long id) {
        SysSupplier sysSupplier = super.getById(id);
        Assert.notNull(sysSupplier, "供应商不存在");
        sysSupplier.setStatus(CommonStatus.DISABLE.code);
        super.updateById(sysSupplier);
    }

    /**
     * 获取供应商下拉列表
     *
     * @return
     */
    @Override
    public List<Select> getSupplierSelect() {
        return super.list(new LambdaQueryWrapper<SysSupplier>()
                        .eq(SysSupplier::getStatus, CommonStatus.ENABLE.code))
                .stream().map(customer -> new Select().setLabel(customer.getName()).setValue(String.valueOf(customer.getId())))
                .collect(Collectors.toList());
    }

    /**
     * 校验供应商信息
     *
     * @param submitDto
     */
    private void checkSupplier(SupplierSubmitDTO submitDto) {

        // 1. 校验客户名称是否重复
        int count = super.count(new LambdaQueryWrapper<SysSupplier>()
                .eq(SysSupplier::getName, submitDto.getName())
                .ne(submitDto.getId() != null, SysSupplier::getId, submitDto.getId()));
        Assert.isTrue(count == 0, "该供应商已存在");

        //2. 校验供应商id是否正确
        if (submitDto.getId() != null) {
            SysSupplier supplier = super.getById(submitDto.getId());
            Assert.notNull(supplier, "供应商不存在");
        }
    }

}
